//
//  FCSuitTableView.m
//  webViewLocal
//
//  Created by WJC on 2021/5/6.
//

#import "FCSuitTableView.h"
#import "NSObject+KVCChangeType.h"

@interface FCSuitTableView()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation FCSuitTableView
-(void)setRequestUseCell:(NSString *)requestUseCell{
    _requestUseCell = requestUseCell;
    [self registerClass:NSClassFromString(requestUseCell) forCellReuseIdentifier:requestUseCell];
}
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.estimatedSectionHeaderHeight=0;
        self.estimatedSectionFooterHeight=0;
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.setTableViewHeadBlock) {
        return self.setTableViewHeadBlock(section ,nil);
    }
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.setTableViewFootBlock) {
        return self.setTableViewFootBlock(section ,nil);
    }
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView =[UIView new];
    if (self.setTableViewHeadBlock) {
        self.setTableViewHeadBlock(section, headView);
    }
    return headView;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.editStyle) {
        return YES;
    }
    return NO;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.editStyle) {
        return self.editStyle;
    }
    return UITableViewCellEditingStyleNone;
}
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.setTableViewAction) {
        return self.setTableViewAction(indexPath);
    }else{
        return nil;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView =[UIView new];
    if (self.setTableViewFootBlock) {
        self.setTableViewFootBlock(section, footView);
    }
    return footView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.numberOfSectionsBlock) {
        return self.numberOfSectionsBlock();
    }
    for (id obj in self.contentModelArr) {
        if ([obj isKindOfClass:[NSArray class]]) {
            return self.contentModelArr.count;
        }
    }
    if(self.numberSection == YES) return self.contentModelArr.count;
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.numberOfRowsInSectionBlock) {
        return self.numberOfRowsInSectionBlock(section);
    }
    for (id obj in self.contentModelArr) {
        if ([obj isKindOfClass:[NSArray class]]) {
            if ([self.contentModelArr indexOfObject:obj] == section) {
                return ((NSArray*)obj).count;
            }
           
        }
    }
    if(self.numberSection == YES){
        for (id obj in self.contentModelArr) {
            if ([obj isKindOfClass:[NSArray class]]) {
                return ((NSArray*)obj).count;
            }
        }
        return 1;
    }

    return self.contentModelArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.heightForRowAtIndexPath) {
        return self.heightForRowAtIndexPath(indexPath);
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.requestUseCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.requestUseCell];
    }
    if (self.cellLevy) {
        id model ;
        for (id obj in self.contentModelArr) {
            if ([obj isKindOfClass:[NSArray class]]) {
                model = self.contentModelArr[indexPath.section][indexPath.row];
                break;
            }
        }if (!model) {
            if (self.numberSection) {
                model = self.contentModelArr[indexPath.section];
            }else{
                model = self.contentModelArr[indexPath.row];
            }
        }
        [cell setValue:model forKey:self.cellLevy];
    }
    if ([cell checkIsExistPropertyWithInstance:cell verifyPropertyName:self.cellHeight andLogAllProperty:NO]) {
        return [[cell valueForKey:self.cellHeight] doubleValue];
    }
    return [self.cellHeight doubleValue];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.cellForRowAtIndexPathBlock) {
        return self.cellForRowAtIndexPathBlock(indexPath);
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.requestUseCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.requestUseCell];
    }
    if (self.cellLevy) {
        id model ;
        for (id obj in self.contentModelArr) {
            if ([obj isKindOfClass:[NSArray class]]) {
                model = self.contentModelArr[indexPath.section][indexPath.row];
                break;
            }
        }if (!model) {
            if (self.numberSection) {
                model = self.contentModelArr[indexPath.section];
            }else{
                model = self.contentModelArr[indexPath.row];
            }
        }
        [cell setValue:model forKey:self.cellLevy];
    }
    if ([self.tableViewDelegate respondsToSelector:@selector(gettableView:andTableViewCell:cellForRowAtIndexPath:)]) {
        return [self.tableViewDelegate gettableView:self andTableViewCell:cell cellForRowAtIndexPath:indexPath];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(self.tableViewDidSelect){
        self.tableViewDidSelect(indexPath);
    }else{
    if ([self.tableViewDelegate respondsToSelector:@selector(tableViewSelect: didSelectRowAtIndexPath:)]) {
        [self.tableViewDelegate tableViewSelect:self didSelectRowAtIndexPath:indexPath];
    }
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(self.tableViewDidScroll){
        self.tableViewDidScroll(scrollView);
    }
}
@end
